package com.gf.controller.user;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gf.bean.User;

import com.gf.service.impl.UserServiceImpl;
import com.gf.utils.Const;


/*
 * 检测用户账户密码是否正确的servlet
 * */

@WebServlet(name = "alluser",urlPatterns = "/checkuser")
public class CheckUserServlet extends HttpServlet {
      static   UserServiceImpl userimpl = null;
      static  {
              userimpl=new UserServiceImpl();

      }

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    response.setHeader("Pragma", "No-cache");
    response.setDateHeader("Expires", 0);
    response.setHeader("Cache-Control", "no-cache");
    PrintWriter out = response.getWriter();
   String user_name=request.getParameter("username");
    String user_password=request.getParameter("password");
    //获取传递过来的用户名和密码
    User user = userimpl.selectByUsername(user_name);
    JSONObject backjson = new JSONObject();

        //如果判断出用户为null直接错误返回结果
        if (user==null){
        //设置错误的状态码并且返回用户名不存在的提示信息
            response.setStatus(330);
            backjson.put("state", 330);
            backjson.put("message", Const.LOGIN_LOGINACCT_ERROR);

        }else {
            //在用户不为null的情况下判断密码是否正确
            if (!user.getUser_password().equals(user_password)) {
                //设置错误的状态码并且返回用户密码错误的提示信息
                response.setStatus(331);
                backjson.put("state", 331);
                backjson.put("message", Const.LOGIN_USERPSWD_ERROR);

            } else {

                //账户和密码全部正确的情况下返回正确的状态码和登陆成功的提示信息
                response.setStatus(200);
                backjson.put("state", 200);
                backjson.put("message", Const.LOGIN_SUCCESS);
                HttpSession session = request.getSession();
                //获取session并将userid和username存放到session当中
                session.setAttribute("userid", user.getId());
                session.setAttribute("username",user_name);
            }

        }



    out.println(JSON.toJSONString(backjson));
    out.flush();
    out.close();



        }

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    PrintWriter out = response.getWriter();
    String user_name=request.getParameter("username");
    User user = userimpl.selectByUsername(user_name);
    JSONObject backjson = new JSONObject();

    //如果判断出用户为null直接返回结果
    if (user==null){

        response.setStatus(200);
        backjson.put("state", 200);
        backjson.put("message", Const.SIGN_LOGINACCT_SUCCESS);

    }else {
        response.setStatus(330);
        backjson.put("state", 330);
        backjson.put("message", Const.SIGN_LOGINACCT_ERROR);
    }



    out.println(JSON.toJSONString(backjson));
    out.flush();
    out.close();

}
        }
